﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAplicaciones1;



namespace interfaz
{
    public partial class FormLogin : Form
    {
        private Sistema sistema;
        public FormLogin()
        {
            InitializeComponent();
            sistema = Sistema.GetSistema();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUser.Text.Trim();
            string password = txtPass.Text.Trim();
            if ((username != "") && (password != ""))
            {
                lblerrorMsj.Visible = false;

                if ((username == "Admin") && (password == "Admin"))
                {
                    Usuario user = sistema.retornaUsuario(username);
                    sistema.Usuario_loguado = user;
                    FormMenu menu = new FormMenu();
                    this.Visible = false;
                    menu.ShowDialog();                    
                }
                else
                {
                    if (sistema.existeUsuario(username, password))
                    {
                        Usuario user = sistema.retornaUsuario(username);
                        sistema.Usuario_loguado = user;
                        FormMenu menu = new FormMenu();
                        this.Visible = false;
                        menu.ShowDialog();
                        
                    }
                    else
                    {
                        lblerrorMsj.Visible = true;
                        lblerrorMsj.Text = "Usuario o contraseña invalida.";
                        lblerrorMsj.ForeColor = Color.Red;
                    }
                }            
            }
            else
            {
                if ((password == "") && (username == ""))
                {
                    lblrequirerdUsr.ForeColor = Color.Red;
                    lblrequirerdPwd.ForeColor = Color.Red;
                    txtUser.Focus();
                }
                else
                {
                    if (password == "")
                    {
                        lblrequirerdPwd.ForeColor = Color.Red;
                        lblrequirerdUsr.ForeColor = Color.Black;
                        txtPass.Focus();
                    }

                    if (username == "")
                    {
                        lblrequirerdUsr.ForeColor = Color.Red;
                        lblrequirerdPwd.ForeColor = Color.Black;
                        txtUser.Focus();
                    }
                }
                lblerrorMsj.Visible = true;
                lblerrorMsj.ForeColor = Color.Red;
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
            Application.Exit();
        }
       
    }
}
